module TusharePro
  class TusharePro
    attr_accessor :headers, :domain, :token

    def initialize
      @domain = "http://api.tushare.pro"
      @token = "caa53c8c845369eada387faf3e6470e551082db46ea6235350f98454"
      @headers = {
        content_type: 'application/json',
        accept: :json,
      }
    end

    def tushare_pro api_name, **options
      payload = {}
      payload[:api_name] = api_name
      payload[:params] = options
      analysis exec payload
    end

    def exec(payload = {})
      payload["token"] = token
      conn = Faraday.new({ url: "http://api.tushare.pro" })
      response = conn.post('', payload.to_json)
      JSON.parse(response.body)
    end

    private

    def analysis object
      result = []
      object["data"]["items"].each do |item|
        result << object["data"]["fields"].zip(item).to_h
      end
      result
    end
  end
end
# 指数专题
# 同花顺概念和行业指数行情
# 同花顺板块指数行情 https://tushare.pro/document/2?doc_id=260
# $TusharePro.tushare_pro("ths_daily", ts_code: "885947.TI")
#
# 指数基本信息 https://tushare.pro/document/2?doc_id=94
# $TusharePro.tushare_pro("index_basic")
#
# 指数日线行情 https://tushare.pro/document/2?doc_id=95  # 000001.SH: 上证指数, 399001.SZ: 深证成指, 399006.SZ: 创业版指数
# $TusharePro.tushare_pro("index_daily", ts_code: "399107.SZ", "start_date": trade_date(10), "end_date": Time.now.strftime("%Y%m%d"))

# 行情数据
# 实时盘口TICK快照(爬虫版) https://tushare.pro/document/2?doc_id=315
# $TusharePro.tushare_pro("realtime_quote", ts_code: "600111")

# 打板专题数据
# 同花顺行业概念板块 -- 同花顺概念和行业指数 https://tushare.pro/document/2?doc_id=259
# $TusharePro.tushare_pro("ths_index", exchange: "A", type: "I")
# $TusharePro.tushare_pro("ths_index", exchange: "A", type: "N")
#
#
#
# 同花顺概念板块成分 https://tushare.pro/document/2?doc_id=261
# $TusharePro.tushare_pro("ths_member", "ts_code": ths_index["ts_code"])
#
# 涨跌停和炸板数据 -- 涨跌停列表（新）https://tushare.pro/document/2?doc_id=298
# $TusharePro.tushare_pro("limit_list_d", "ts_code": stock["code"], "limit_type": "D", "start_date": trade_time30, "end_date": Time.now.strftime("%Y%m%d"))

# 资金流向数据
# 个股资金流向 https://tushare.pro/document/2?doc_id=170
# $TusharePro.tushare_pro("moneyflow", ts_code: ths_index["ts_code"],  "start_date": trade_time30, "end_date": Time.now.strftime("%Y%m%d"))
#
# 个股资金流向（THS）https://tushare.pro/document/2?doc_id=348
# $TusharePro.tushare_pro("moneyflow_ths", ts_code: st["con_code"], "start_date": trade_time20, "end_date": Time.now.strftime("%Y%m%d"))
#
# 行业资金流向（THS）
# $TusharePro.tushare_pro("moneyflow_ind_ths", ts_code: ts_code, "start_date": trade_time20, "end_date": Time.now.strftime("%Y%m%d"))
# 板块资金流向（THS）
# $TusharePro.tushare_pro("moneyflow_cnt_ths", ts_code: ts_code, "start_date": trade_time20, "end_date": Time.now.strftime("%Y%m%d"))

# 参考数据
# 限售股解禁
# $TusharePro.tushare_pro("share_float", "start_date": Time.now.strftime("%Y%m%d"), "end_date": (Time.now + 188.days).strftime("%Y%m%d")).pluck("ts_code").uniq

# $TusharePro.tushare_pro("trade_cal", "exchange": "SSE", "start_date": "20220403", "end_date": "20220403")
# $TusharePro.tushare_pro("fina_indicator", "ts_code": "600011.SH")
# stock_basic = $TusharePro.tushare_pro "stock_basic"

# $TusharePro.tushare_pro("stk_factor", "ts_code": "600011.SH", "trade_date": "20220526")
# stk_factor_pro = $TusharePro.tushare_pro("stk_factor_pro", "ts_code": "600011.SH", "trade_date": "20220526")
# $TusharePro.tushare_pro("fina_indicator", "ts_code": "600011").first
# $TusharePro.tushare_pro("index_daily"rake ths:choice_block, "ts_code": "399300")
# $TusharePro.tushare_pro("limit_list")

# $TusharePro.tushare_pro("cyq_chips", "ts_code": "601606.SH", "start_date": '20250318', "end_date": '20250620')  筹码

# get_k_data = Tushare::Stock::Trading.get_k_data("600011", "2025-03-14", "2025-03-14")
# realtime_stock_info = Tushare::Stock::Trading.get_k_data "600011"